/** Copyright (c) 2012, peter.ducai@gmail.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Peter Ducai nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL PETER "daemonna" DUCAI BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.daemonna.fluentlee_core.reporting;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * @author peter 'daemonna' ducai @author peter.ducai@gmail.com @author
 * http://code.google.com/p/fluentlee/
 */
public class HTMLprocessor {

    FileWriter fstream;
    BufferedWriter out;
    public int table_width = 800;
    public String sum_flow_name = null;
    public String sum_status = null;
    public String sum_start_time = null;
    public String sum_end_time = null;
    public String sum_elapsed = null;

    public HTMLprocessor(String filename) {
        try {
            fstream = new FileWriter(new File(filename));  //add date to filename
            out = new BufferedWriter(fstream);
        } catch (IOException ex) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write_link(String name, String url) {
        try {
            out.newLine();
            out.write("<a href=\"" + url + "\">" + name + "</a>");
            out.newLine();
        } catch (IOException ex) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write_br() {
        try {
            out.newLine();
            out.write("<br/>");
            out.newLine();
        } catch (IOException ex) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write_HEAD(String name) {
        try {

            out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
            out.newLine();
            out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            out.newLine();
            out.write("<head>");
            out.newLine();
            out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />");
            out.write("<meta http-equiv=\"refresh\" content=\"600\">");
            out.newLine();
            out.write("<title>Test Report from " + name + ".schedule</title>");
            out.newLine();
            out.write("<style>");
            out.newLine();
            out.write("body {");
            out.newLine();
            out.write("font: bold 10px \"Trebuchet MS\", Verdana, Arial, Helvetica, sans-serif;");
            out.newLine();
            out.write("color: #4f6b72;");
            out.newLine();
            out.write("background: #E6EAE9;");
            out.write("}");
            out.newLine();
            out.write("a {");
            out.newLine();
            out.write("color: #c75f3e;");
            out.newLine();
            out.write("}");
            out.newLine();
            out.write("caption {");
            out.write("padding: 0 0 5px 0;");
            out.write("width: 1200px;");
            out.write("font: bold 16px \"Trebuchet MS\", Verdana, Arial, Helvetica, sans-serif;");
            out.write("text-align: right;");
            out.write("}");
            out.newLine();
            out.write("caption.summary {");
            out.write("padding: 0 0 5px 0;");
            out.write("font: bold 16px \"Trebuchet MS\", Verdana, Arial, Helvetica, sans-serif;");
            out.write("text-align: left;");
            out.write("}");
            out.newLine();
            out.write("th.sum {");
            out.write("background: #000000;");
            out.write("color: #ffffff;");
            out.write("}");
            out.newLine();
            out.write("th {");
            out.write("color: white;");
            out.write("border-right: 1px solid #C1DAD7;");
            out.write("border-bottom: 1px solid #C1DAD7;");
            out.write("border-top: 1px solid #C1DAD7;");
            out.write("letter-spacing: 2px;");
            out.write("text-transform: uppercase;");
            out.write("text-align: left;");
            out.write("padding: 6px 6px 6px 12px;");
            out.write("background: grey;");
            out.write("}");
            out.newLine();
            out.write("th.tname {");
            out.write("color: black;");
            out.write("border-right: 1px solid #C1DAD7;");
            out.write("border-bottom: 1px solid #C1DAD7;");
            out.write("border-top: 1px solid #C1DAD7;");
            out.write("letter-spacing: 2px;");
            out.write("text-transform: uppercase;");
            out.write("text-align: left;");
            out.write("padding: 6px 6px 6px 12px;");
            out.write("background: #66a3d2;");
            out.write("}");
            out.newLine();
            out.write("td {");
            out.write("border-right: 1px solid #C1DAD7;");
            out.write("border-bottom: 1px solid #C1DAD7;");
            out.write("background: #fff;");
            out.write("padding: 6px 6px 6px 12px;");
            out.write("color: #4f6b72;");
            out.write("}");
            out.newLine();
            out.write("td.fail {");
            out.newLine();
            out.write("background-color: red;");
            out.newLine();
            out.write("color: white;");
            out.newLine();
            out.write("}");
            out.newLine();
            out.write("td.pass {");
            out.newLine();
            out.write("background-color: green;");
            out.newLine();
            out.write("color: white;");
            out.newLine();
            out.write("}");
            out.newLine();
            out.write("</style>");
            out.newLine();
            out.write("</head>");
            out.newLine();
            out.write("<body>");
            out.newLine();

        } catch (IOException ex) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //TODO to do newlines

    public void write_FOOT() {

        try {
            out.newLine();
            out.write("</body></html>");
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void start_table(int width) {
        try {
            table_width = width;
            out.newLine();
            out.write("<table width=\"" + table_width + "px\" cellspacing=\"0\" summary=\"Test Report\">");
            out.newLine();
        } catch (IOException e) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void write_table_header(String[] vals) {
        try {
            out.write("<tr>");
            out.newLine();

            for (int z = 0; z < vals.length; z++) {
                out.write("<th>" + vals[z] + "</th>");
                out.newLine();
            }

            out.write("</tr>");
            out.newLine();
        } catch (IOException e) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void write_table_row(String[] vals) {
        try {
            out.write("<tr>");
            out.newLine();

            for (int z = 0; z < vals.length; z++) {
                if (vals[z] != null) {
                    if(vals[z].contains("class")){
                        out.write("<td" + vals[z] + "</td>");
                    } else {
                        out.write("<td>" + vals[z] + "</td>");
                    }
                    out.newLine();
                }
            }

            out.write("</tr>");
            out.newLine();
        } catch (IOException e) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void end_table() {
        try {
            out.newLine();
            out.write("</table>");
            out.newLine();
        } catch (IOException e) {
            Logger.getLogger(HTMLprocessor.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}
